package com.padakeji.android.pworks.example;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.padakeji.android.pworks.app.PadaActivity;
import com.padakeji.android.pworks.content.PadaImageLoaderFactory;
import com.padakeji.android.pworks.content.SharedPreferencesHelper;
import com.padakeji.android.pworks.example.nav.NavTestActivity;
import com.padakeji.android.pworks.example.retrofit.RetrofitTestActivity;
import com.padakeji.android.pworks.example.retrofit.bean.GithubUser;
import com.padakeji.android.pworks.example.retrofit.bean.TestBean;
import com.padakeji.android.pworks.module.image.view.ImagePickerActivity;
import com.padakeji.android.pworks.module.version.VersionController;
import com.padakeji.android.pworks.module.version.entity.VersionInfo;
import com.padakeji.android.pworks.ui.UIHelper;

import java.util.List;

public class MainActivity extends PadaActivity {

	private final static int IMAGE_PICKER_REQUEST_CODE = 1001;

    private Button imagePicker;
	private ImageView image;

	private Button retrofitTextBtn;
	private Button navTestBtn;
	private Button spTestBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		new Thread(new Runnable() {
			@Override
			public void run() {
				VersionController vc = new VersionController(getApplicationContext()){
					@Override
					public VersionInfo checkRemoteVersion() {
						VersionInfo vi = new VersionInfo();
						vi.versionCode = 1;
						vi.versionName = "1.1";
						vi.downloadLink = "";
						vi.releaseNotes = "1. 高大上的UI设计\n2. 屌炸天的性能提升";
						return vi;
					}
				};
				vc.checkUpdate(true);
			}
		}).start();

		image = (ImageView) findViewById(R.id.activity_main_image_show);
        imagePicker = (Button) findViewById(R.id.activity_main_image_picker);
        imagePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, ImagePickerActivity.class);
				startActivityForResult(i, IMAGE_PICKER_REQUEST_CODE);
            }
        });

        retrofitTextBtn = (Button) findViewById(R.id.activity_main_btn_retrofit_test);
        retrofitTextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, RetrofitTestActivity.class);
                startActivity(i);
            }
        });

        navTestBtn = (Button) findViewById(R.id.activity_main_btn_nav_test);
        navTestBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, NavTestActivity.class);
                startActivity(i);
            }
        });

		spTestBtn = (Button) findViewById(R.id.activity_main_btn_testSP);
		spTestBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {

                    GithubUser u = new GithubUser();
                    u.id = "123445";
                    u.login = "atanl";
                    TestBean tb = new TestBean();
                    tb.a = "a";
                    tb.b = "b";
                    SharedPreferencesHelper spHelper = new SharedPreferencesHelper(MainActivity.this);
                    String key = "user";
                    String key1 = "testb";
                    String keyInt = "inttest";
                    spHelper.save(keyInt, 1);
                    spHelper.save(key, u);
                    spHelper.save(key1, tb);
                    String booleanKey = "booleanKey";
                    spHelper.save(booleanKey, false);
                    GithubUser gu = spHelper.get(key);
                    TestBean tbb = spHelper.get(key1);
                    int inta = spHelper.get(keyInt);
                    boolean bt = spHelper.get(booleanKey);
                    new UIHelper(MainActivity.this).showToast(gu.login);
                } catch (Exception e) {
                    e.printStackTrace();
                }

			}
		});

	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == IMAGE_PICKER_REQUEST_CODE) {
			if (resultCode == RESULT_OK) {
				List<String> paths = data.getStringArrayListExtra(ImagePickerActivity.RESULT_DATA);
				if (paths != null && paths.size() > 0) {
                    new UIHelper(this).showToast(paths.get(0));
					PadaImageLoaderFactory.getImageLoader(this).display("file://" + paths.get(0), image);
				}
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
